package com.smasher.certificate.register

import com.smasher.certificate.extension.JksPluginExtension
import com.smasher.certificate.extractor.SignatureExtractor
import org.gradle.api.Project

class TaskRegister {


    static def registerApkTask(Project target) {
        target.tasks.register('executeApkSignatureInfo') {
            // 设置任务组和描述
            group = 'certificate'
            description = '从APK文件中提取签名信息'

            doLast {
                // 获取所有变体的APK路径
                def apkPaths = QueryApkPath.getAllApkPaths(target)
                if (apkPaths.isEmpty()) {
                    println "未找到任何变体的APK文件"
                    return
                }

                println "\n=== 找到 ${apkPaths.size()} 个APK文件 ==="
                apkPaths.each { variantName, path ->
                    println "\n处理变体: ${variantName}"
                    println "APK路径: ${path}"
                    try {
                        SignatureExtractor.extractSignatureFromApk(path)
                    } catch (Exception e) {
                        println "处理 ${variantName} 变体时出错: ${e.message}"
                        e.printStackTrace()
                    }
                    println "\n" + "=".repeat(50)
                }
            }
        }
    }

    static def registerJKSInfoTask(Project target) {

        def extension = target.getExtensions().getByType(JksPluginExtension.class)

        target.tasks.register('executeJKSInfo') {
            // 设置任务组和描述
            group = 'certificate'
            description = '从JKS文件中提取签名信息'

            doLast {
                // 参数验证
                if (!extension.jksPath) {
                    println "错误: 未设置JKS文件路径"
                    println "提示: 在build.gradle中配置JKSParse.jksPath或确保该属性已正确设置"
                    return
                }

                if (!extension.password) {
                    println "错误: 未设置JKS密码"
                    println "提示: 在build.gradle中配置JKSParse.password或确保该属性已正确设置"
                    return
                }

                if (!extension.alias) {
                    println "错误: 未设置证书别名"
                    println "提示: 在build.gradle中配置JKSParse.alias或确保该属性已正确设置"
                    return
                }

                // 验证JKS文件是否存在
                def jksFile = new File(extension.jksPath)
                if (!jksFile.exists()) {
                    println "错误: JKS文件不存在: ${extension.jksPath}"
                    println "提示: 请检查JKS文件路径是否正确"
                    return
                }

                if (!jksFile.isFile()) {
                    println "错误: JKS路径不是一个有效的文件: ${extension.jksPath}"
                    println "提示: 请提供一个有效的JKS文件路径"
                    return
                }

                try {
                    SignatureExtractor.extractFromJKS(extension.jksPath, extension.password, extension.alias)
                } catch (Exception e) {
                    println "处理JKS文件时出错: ${e.message}"
                    e.printStackTrace();
                }
            }
        }
    }


}